<?php

class Application_Model_KnowUser extends Application_Model_DomainObject {
    
	protected $_id;
        
        protected $_backlogId;
        
	protected $_userId;
        
        protected $_type;
        
        protected $_dateCreated;
        /**
         * 客服记录
         */
        const Type_Service = 1;
        /**
         * 待办事务
         */
        const Type_Backlog = 2;
        
        public static function getAllByBacklogIdType($backlogId,$type)
        {
            return self::getDao()->queryAll(array(" backlogId = "=>$backlogId," and type = " => $type));
        }

        public static function addKnowUser($backlogId,$userId,$type)
        {
            $knowUserItem = new Application_Model_KnowUser();
            $knowUserItem->BacklogId = $backlogId;
            $knowUserItem->UserId = $userId;
            $knowUserItem->Type = $type;
            $knowUserItem->DateCreated = date("Y-m-d H:i:s");
            $knowUserItem->save();
        }
        
        public static function delKnowUser($backlogId)
        {
            $arrKnowUser = self::getDao()->queryAll(array("backlogId"=>$backlogId));
            foreach ($arrKnowUser as $knowUserVal) {
                $knowUserVal->delete();
            }
        }
        
        public static function getKnowAll($userId,$starttime,$endtime,$pagesize,$pageindex)
        {
            $where[" userId = "] = $userId;
            $and = " and ";
            if($starttime != "")
            {
                $where[$and." dateCreated >= "] = $starttime;
                $and = " and ";
            }
            if($endtime != "")
            {
                $where[$and." dateCreated <= "] = $endtime;
                $and = " and ";
            }
            return self::getDao()->queryPage($where, " id desc", $pagesize, $pageindex);
        }
        
        private $_relevanceItem;
        public function getRelevanceKnowUser()
        {
            if ($this->_relevanceItem == NULL)
            {
                switch ($this->_type) {
                    case self::Type_Service:
                        $this->_relevanceItem = Application_Model_Service::load($this->_backlogId);
                        break;
                    case self::Type_Backlog:
                        $this->_relevanceItem = Application_Model_Backlog::load($this->_backlogId);
                        break;
                }
            }
            return $this->_relevanceItem;
        }
        
        public function getContent()
        {
            $relevanceItem = $this->getRelevanceKnowUser();
            switch ($this->_type) {
                case self::Type_Service:
                    return $relevanceItem->Content;
                    break;
                case self::Type_Backlog:
                    return $relevanceItem->Content;
                    break;
            }
            return null;
        }
        
        public function getUrl()
        {
            switch ($this->_type) {
                case self::Type_Service:
                    return "/admini/manageadmin/servicedetail/id/".$this->_backlogId;
                    break;
                case self::Type_Backlog:
                    return "/admini/manageadmin/backlogdetail/id/".$this->_backlogId;
                    break;
            }
            return "/";
        }
        
        public function getTypeName()
        {
            switch ($this->_type) {
                case self::Type_Service:
                    return "客服记录";
                    break;
                case self::Type_Backlog:
                    return "待办事务";
                    break;
            }
            return "/";
        }
        
        public function getOperator()
        {
            $relevanceItem = $this->getRelevanceKnowUser();
            switch ($this->_type) {
                case self::Type_Service:
                    return $relevanceItem->Operator;
                    break;
                case self::Type_Backlog:
                    return $relevanceItem->Operator;
                    break;
            }
            return null;
        }
}